﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Configuration;
using System.Data.SqlClient;
using System.Text;
using Common.Utilities;
using Common.Utilities.DLR;
using Workflows.Components.Monitor;

namespace Workflows.Components.DALC
{
    /// <summary>
    /// this class is non-transactional, do not obtain locks on target table 
    /// and run in async mode
    /// </summary>
    public class LoggerDALC
    {
        #region pub static methods
        /// <summary>
        /// 
        /// </summary>
        /// <param name="log"></param>
        public static void LogWfDesign(WfDesignLog log)
        {
            BackgroundWorker designLogWorker = new BackgroundWorker();
            designLogWorker.DoWork += new DoWorkEventHandler(designLogWorker_DoWork);
            designLogWorker.RunWorkerAsync(log);
        }

        /// <summary>
        /// 
        /// </summary>
        /// <param name="log"></param>
        public static void LogWfRun(WfRunLog log)
        {
            BackgroundWorker runLogWorker=new BackgroundWorker();
            runLogWorker.DoWork += new DoWorkEventHandler(runLogWorker_DoWork);
            runLogWorker.RunWorkerAsync(log);
        }

        #endregion

        #region async methods
        static void designLogWorker_DoWork(object sender, DoWorkEventArgs e)
        {
            WfDesignLog log = (WfDesignLog)e.Argument;
            string connStr = ConfigurationManager.ConnectionStrings["Workflow"].ConnectionString;
            try
            {
                ORMapping<WfDesignLog> orm = new ORMapping<WfDesignLog>();
                orm.SaveToDB(ref log, connStr);
            }
            catch (Exception ex)
            {
                ExceptionHandler.HandlesException(ex);
            }
        }

        static void runLogWorker_DoWork(object sender, DoWorkEventArgs e)
        {
            WfRunLog log = (WfRunLog) e.Argument;
            string connStr = ConfigurationManager.ConnectionStrings["Workflow"].ConnectionString;
            try
            {
                ORMapping<WfRunLog> orm = new ORMapping<WfRunLog>();
                orm.SaveToDB(ref log,connStr);
            }
            catch (Exception ex)
            {
                ExceptionHandler.HandlesException(ex);
            }
        }
        #endregion
    }
}
